
class QznnProxy extends ResourceProxyBase {
    public static NAME: string = "QznnProxy";//必须和excel导出文件一致
    public static Proxy: QznnProxy = null;
    public constructor() {
        super(QznnProxy.NAME);
    }
    public static getProxy() {
        if (!QznnProxy.Proxy) {
            QznnProxy.Proxy = new QznnProxy();
        }
        return QznnProxy.Proxy;
    }

    private relink: boolean = false;

    //登录成功 1100
    public login(data: Object) {
        QznnVO.VO = null;
        QznnVO.getVo().lUserScore = GameVO.getVo().lUserScore;
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.OPEN_PLAY_WAIT_GAME);
    }

    public Relink(): void {
        this.relink = true;
    }

    //获取大房间信息 2101
    public getRoomInfo(data: Object) {
        QznnVO.getVo().servertype = data["dwServerRule"];
        QznnVO.getVo().roomChairNum = data["wChairCount"];
        QznnVO.getVo().servertype = data["wServerType"];
        QznnVO.getVo().roomNum = data["wTableCount"];
    }
    //用户战绩信息刷新 包括金币 3-101
    public getUserInfoGold(data: Object) {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        for (let i in QznnVO.getVo().otherPlayer) {
            let user = QznnVO.getVo().otherPlayer[i];
            if (user.dwUserID == dwUserID) {
                user.lScore = lScore;
                break;
            }
        }
        this.updataUsersScore(data)
    }
    //加入游戏大房间 3100
    public joinRoom(data: Object) {
        let user = data["tagMobileUserInfoHead"];
        QznnVO.getVo().otherPlayer[user.dwUserID] = user;//广场所有人存入集合
        this.userJoinRoom();


        // let user = data["tagMobileUserInfoHead"];
        // QznnVO.getVo().otherPlayer.push(user);
    }

    public updataUsersScore(data: Object): void {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        let user = QznnVO.getVo().otherPlayer[dwUserID];
        if (user) {
            user.lScore = lScore;
        }
        this.userJoinRoom();
    }

    //获取用户信息 3102
    public getUserInfo(data: Object) {
        //用户状态发生改变从缓存的所有用户中找到该用户改变其座位
        let dwUserID = data["dwUserID"];
        let wTableID = data["tableId"];
        let wChairID = data["wChairID"];
        let cbUserStatus = data["cbUserStatus"];
        let user = QznnVO.getVo().otherPlayer[dwUserID]
        if (user) {
            user.wTableID = wTableID;
            user.wChairID = wChairID;
            user.cbUserStatus = cbUserStatus;
        }
        this.userJoinRoom();
    }
    public userJoinRoom() {
        QznnVO.getVo().initUserInof();
        //状态改变完毕后从所有玩家中找自己是否已入座，若自己入座，则找是否跟自己同桌的用户，将其显示到桌子对面
        let user = QznnVO.getVo().otherPlayer[GameVO.getVo().dwUserID]
        if (user) {
            QznnVO.getVo().wTableID = user.wTableID;
            QznnVO.getVo().wChairID = user.wChairID;
            QznnVO.getVo().myReadyStatus = user.cbUserStatus
            QznnVO.getVo().lUserScore = user.lScore
        }
        //我入座找对手
        if (QznnVO.getVo().wTableID != null && QznnVO.getVo().wTableID != 65535) {
            QznnVO.getVo().wTableId_up = QznnVO.getVo().wTableID
            //根据我所在椅子确定其他玩家在桌面坐哪个位置
            let user2 = (QznnVO.getVo().wChairID + 1) % 4
            let user3 = (QznnVO.getVo().wChairID + 2) % 4
            let user0 = (QznnVO.getVo().wChairID + 3) % 4
            for (let i in QznnVO.getVo().otherPlayer) {
                let user = QznnVO.getVo().otherPlayer[i]
                if (user.wTableID == QznnVO.getVo().wTableID &&
                    user.dwUserID != GameVO.getVo().dwUserID) {//跟我坐一桌的就是对手
                    if (user.wChairID == user2) {
                        QznnVO.getVo().rightChairID = user.wChairID;
                        QznnVO.getVo().rightID = user.dwUserID
                        QznnVO.getVo().rightSource = user.wFaceID
                        QznnVO.getVo().rightName = user.nickName
                        QznnVO.getVo().rightGlod = user.lScore;
                        QznnVO.getVo().rightReadyStatus = user.cbUserStatus
                    }
                    if (user.wChairID == user3) {
                        QznnVO.getVo().upChairID = user.wChairID
                        QznnVO.getVo().upID = user.dwUserID
                        QznnVO.getVo().upSource = user.wFaceID
                        QznnVO.getVo().upName = user.nickName
                        QznnVO.getVo().upGlod = user.lScore
                        QznnVO.getVo().upReadyStatus = user.cbUserStatus
                    }
                    if (user.wChairID == user0) {
                        QznnVO.getVo().leftChairID = user.wChairID
                        QznnVO.getVo().leftID = user.dwUserID
                        QznnVO.getVo().leftSource = user.wFaceID
                        QznnVO.getVo().leftName = user.nickName
                        QznnVO.getVo().leftGlod = user.lScore;
                        QznnVO.getVo().leftReadyStatus = user.cbUserStatus
                    }
                }
            }
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.JOIN_ROOM);
        } else {
            if (QznnVO.getVo().wTableId_up != null && QznnVO.getVo().wTableID == 65535) {
                //上次有座位此次没座位说明被踢了
                SocketManager.closeSocket(InstructionsEnum.GAME_TYPE_NAME.QZNN);
            }

        }
    }
    //重新进游戏数据恢复
    public dataRecovery(data: Object) {
        if (RegDataQznn.MainCMD_MsgId[100][101] = RegDataQznn.free) {
            this.gameStandby(data)
        }
        if (RegDataQznn.MainCMD_MsgId[100][101] == RegDataQznn.gameCall) {
            this.dataRecoveryByCall(data)
        }
        if (RegDataQznn.MainCMD_MsgId[100][101] == RegDataQznn.gameScore) {
            this.dataRecoveryByPlay(data);
        }
        if (RegDataQznn.MainCMD_MsgId[100][101] == RegDataQznn.gamePlay) {
            this.dataRecoveryByShow(data)
        }
    }

    //游戏空闲状态 9106
    public gameStandby(data: Object): void {
        if (data) {
            QznnVO.getVo().difen = data["lCellScore"];
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.SETDIFEN);
        }
    }
    public dataRecoveryByCall(data: Object) {//叫分
        QznnVO.getVo().RelinkTime = data["cbCurrentTimer"];
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINQIANGZHUANG);
    }
    public dataRecoveryByPlay(data: Object) {//下注
        QznnVO.getVo().RelinkTime = data["cbCurrentTimer"];

        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINXIAZHU);
    }
    public dataRecoveryByShow(data: Object) {//摊牌
        QznnVO.getVo().RelinkTime = data["cbCurrentTimer"];

        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINTANPAI);
    }
    //游戏开始 9100
    public StartGame(data: Object): void {
        QznnVO.getVo().maxScore = data["lTurnMaxScore"]
        if (data) {
            this.qiangZhuangResult(data);
        }
    }

    //抢庄结果 
    public qiangZhuangResult(data: Object): void {
        QznnVO.getVo().zhuangID = data["wBankerUser"];
        if (data) {
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.SINGLE_PLAYER_QIANG_ZHUANG, data);
        }
    }

    //闲家下注 9111
    public Xianjiaxiazhu(data: Object): void {
        //加注倍数cbAddTimes
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.XIANJIA_XIAZHU, data);
    }

    //所有玩家亮手牌 200-104
    public ShowHandCard(data): void {
        let tableScore = data["lGameScore"];
        if (tableScore[QznnVO.getVo().zhuangID] > 0) {
            let results = 0;
            for (let i = 0; i < tableScore.length; i++) {
                let singledate = 0;
                if (i == QznnVO.getVo().zhuangID) {
                    singledate = tableScore[i] / 95 * 100
                }
                else {
                    singledate = tableScore[i]
                }
                results = results + singledate;
            }
            if (!results) {
                QznnVO.getVo().typeAni = 1;
            }
            else {
                if (tableScore[QznnVO.getVo().wChairID] > 0) {
                    QznnVO.getVo().typeAni = 3;
                }
            }
        }
        else if (tableScore[QznnVO.getVo().zhuangID] < 0) {
            let results = 0;
            for (let i = 0; i < tableScore.length; i++) {
                let singledate = 0;
                if (i == QznnVO.getVo().zhuangID) {
                    singledate = tableScore[i] / 100 * 95
                }
                else {
                    singledate = tableScore[i];
                }
                results = results + singledate;
            }
            if (!results) {
                QznnVO.getVo().typeAni = 2;
            }
            else {
                if (tableScore[QznnVO.getVo().wChairID] > 0) {
                    QznnVO.getVo().typeAni = 3;
                }
            }
        }
        QznnVO.getVo().my_win = data["lGameScore"][QznnVO.getVo().wChairID];
        QznnVO.getVo().left_win = data["lGameScore"][QznnVO.getVo().leftChairID]
        QznnVO.getVo().right_win = data["lGameScore"][QznnVO.getVo().rightChairID]
        QznnVO.getVo().up_win = data["lGameScore"][QznnVO.getVo().upChairID]

        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.SHOW_ALLCARD);
    }

    //叫庄 101
    public playerAddTimes(data: Object): void {
        if (data) {
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.PLAYER_ADDTIME, data);
        }
    }


    //获取牌局编号
    public getSerialNumber(data: Object) {
        QznnVO.getVo().gameNumbering = data["szSerialNumber"];
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.GET_SERIAL_NUMBER)
    }
    //系统发牌 200-103
    public sysSendCard(data: Object): void {
        QznnVO.getVo().allplayerhandcard = data["cbHandCardData"];
        QznnVO.getVo().niutype = data["cbCardType"];
        if (data) {
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.SHOWCARD, data);
        }
    }

    //单局结算 9104
    public singleEnd(data: Object): void {
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.SINGLE_GAME_END, data);
    }

    //玩家摊牌 9105
    public playerShowCard(data: Object): void {
        if (data) {
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.WANJIATAMPAI, data["cbChairID"]);
        }
    }

    public gameJiaoZHuang(data: Object): void {
        if (data) {
            // QznnVO.getVo().bExit = false;
            // let obj1 = new Object();
            // obj1["dwUserID"] = GameVO.getVo().dwUserID;
            // game.AppFacade.instance.sendNotification(PanelNotify.QZNN.PLAYER_SITDOWN, obj1);
            QznnVO.getVo().playerstate = data["cbPlayStatus"];
            QznnVO.getVo().qiangzhuangtime = data["wTimeCallBanker"];
            QznnVO.getVo().jiazhutime = data["wTimeCallScore"];
            QznnVO.getVo().tanpaitime = data["wTimeOpenCard"];

            if (data["wTimeLeftCallBanker"]) {
                QznnVO.getVo().RelinkTime = data["wTimeLeftCallBanker"];
            }
            else {
                QznnVO.getVo().RelinkTime = data["wTimeCallScore"];
            }
            this.relink = false;
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINQIANGZHUANG, data);
        }
    }

    public gameXiaZhu(data): void {
        if (data) {
            // QznnVO.getVo().bExit = false;
            QznnVO.getVo().qiangzhuangtime = data["wTimeCallBanker"];
            QznnVO.getVo().jiazhutime = data["wTimeCallScore"];
            QznnVO.getVo().tanpaitime = data["wTimeOpenCard"];
            QznnVO.getVo().playerstate = data["cbPlayStatus"];
            if (data["wTimeLeftCallBanker"]) {
                QznnVO.getVo().RelinkTime = data["wTimeLeftCallBanker"];
            }
            else {
                QznnVO.getVo().RelinkTime = data["wTimeOpenCard"];
            }
            this.relink = false;
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINXIAZHU, data);
        }
    }

    public gameTanpai(data: Object): void {
        if (data) {
            // QznnVO.getVo().bExit = false;
            QznnVO.getVo().qiangzhuangtime = data["wTimeCallBanker"];
            QznnVO.getVo().jiazhutime = data["wTimeCallScore"];
            QznnVO.getVo().tanpaitime = data["wTimeOpenCard"];
            QznnVO.getVo().playerstate = data["cbPlayStatus"];
            if (data["wTimeLeftCallBanker"]) {
                QznnVO.getVo().RelinkTime = data["wTimeLeftCallBanker"];
            }
            else {
                QznnVO.getVo().RelinkTime = data["wTimeOpenCard"];
            }
            this.relink = false;
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.RELINKINTANPAI, data);
        }
    }

    public gamePinpai(data: Object): void {
        if (data) {
            // QznnVO.getVo().bExit = false;
            QznnVO.getVo().qiangzhuangtime = data["wTimeCallBanker"];
            QznnVO.getVo().jiazhutime = data["wTimeCallScore"];
            QznnVO.getVo().tanpaitime = data["wTimeOpenCard"];
            QznnVO.getVo().playerstate = data["cbPlayStatus"];
            if (data["wTimeLeftCallBanker"]) {
                QznnVO.getVo().RelinkTime = data["wTimeLeftCallBanker"];
            }
            else {
                QznnVO.getVo().RelinkTime = data["wTimeOpenCard"];
            }
            this.relink = false;
        }
    }

    //游戏玩家准备 9113
    public playerPrepare(data: Object): void {
        // if (data) {
        //     game.AppFacade.instance.sendNotification(PanelNotify.QZNN.PLAYER_PREPARE, data);
        // }
        // else {
        game.AppFacade.instance.sendNotification(PanelNotify.QZNN.CLEAR_TABLE, data);
        // }
    }
}
